<?php
namespace app\common\model;

use app\common\model\UserDownloadTimes;
use app\common\model\UserMain;
use app\common\model\UserSiteAccess;
use think\facade\Db;
use think\facade\Request;
use think\Model;

class UserCard extends Model
{
    protected $pk        = 'card_id';
    protected $json      = ['vip_access', 'download_times'];
    protected $jsonAssoc = true;
    protected $type      = [
        'create_time' => 'timestamp',
        'update_time' => 'timestamp',
    ];

    public static function onBeforeInsert($data)
    {
        $data->create_time = Request::time();
        $data->create_ip   = Request::ip();
        $data->status      = 1;
    }

    public function getTypeTextAttr($value, $data)
    {
        switch ($data['type']) {
            case '0':
                return '体验卡';
                break;
            case '1':
                return '周卡';
                break;
            case '2':
                return '月卡';
                break;
			case '3':
			    return '年卡';
			    break;
			case '-1':
			    return '终身';
			    break;
            default:
                return '未知';
                break;
        }
    }

    public function getOutTimeFormatAttr($value, $data)
    {
        if ($data['out_time'] <= 0) {
            return '永久';
        }
        return date('Y-m-d H:i:s', $data['out_time']);
    }

    public function getStatusTextAttr($value, $data)
    {
        switch ($data['status']) {
            case -1:
                return '失效';
                break;
            case 0:
                return '已使用';
                break;
            case 1:
                return '可用';
                break;

            default:
                return '未知';
                break;
        }
    }

    public function exchange_card($uid = 0)
    {
        $updateInfo  = [
            'vip_type' => $this->getAttr('type')
        ];

        $row = UserMain::where('uid', '=', $uid)->find();

        $oldExpireTime = $row['vip_expire_time'];

        if (!empty($oldExpireTime) && strtotime($oldExpireTime) > time()){
            /**
             * 判断当前用户的过期时间是否大于当前时间
             *  大于当前时间则直接在原始过期时间 + 充值天数
             *  已过期的话 直接在当前时间 + 充值天数
             */

            if (strtotime($oldExpireTime) > time()){
                switch ($this->getAttr('type')){
                    case 0:
                        $vipExpireTime = strtotime('+1 days', strtotime($oldExpireTime));
                        break;
                    case 1:
                        $vipExpireTime = strtotime('+1 weeks', strtotime($oldExpireTime));
                        break;
                    case 2:
                        $vipExpireTime = strtotime('+1 month', strtotime($oldExpireTime));
                        break;
                    case 3:
                        $vipExpireTime = strtotime('+1 years', strtotime($oldExpireTime));
                        break;
                    case -1:
                        $vipExpireTime = 0;
                        break;
                    default:
                        break;
                }
            }
        }else{
            switch ($this->getAttr('type')){
                case 0:
                    $vipExpireTime = strtotime('+1 days');
                    break;
                case 1:
                    $vipExpireTime = strtotime('+1 weeks');
                    break;
                case 2:
                    $vipExpireTime = strtotime('+1 month');
                    break;
                case 3:
                    $vipExpireTime = strtotime('+1 years');
                    break;
                case -1:
                    $vipExpireTime = -1;
                    break;
                default:
                    break;
            }
        }

        $updateInfo['vip_expire_time'] = $vipExpireTime;

        UserMain::where('uid', '=', $uid)->update($updateInfo);
    }
}
